<?php
class PermissionAction extends AdminAction{
	//RBAC权限节点设置

	//节点列表
	public function index(){

		$node=M('Node');

		$list=$node->select();

		$j=count($list);
		for($i=0;$i<$j;$i++){
			$list[$i]['statusname']=($list[$i]['status']==1)?'启用':'禁用';
			$level=$this->level();			
			$list[$i]['levelname']=$level[$list[$i]['level']];
		}

		$this->list=$list;

		$this->display();

	}

	//编辑节点
	public function edit(){
		$node=M('Node');

		$list=$node->where('id='.$_GET['id'])->find();
		$this->list=$list;

		$this->pidList=$this->pid();
		
		$this->levelselect=$this->level();
		$this->display();
	}
	public function editing(){
		$node=M('Node');
		$res=$node->save($_POST);
		if($res){
			$this->success('编辑成功','index');
		}else{
			$this->error('编辑失败');
		}
	}

	//删除节点
	public function del(){
		$node=M('Node');
		$res=$node->delete($_GET['id']);
		if($res){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	//节点添加
	public function add(){
		$this->pidList=$this->pid();
		$this->levelselect=$this->level();
		$this->display();
	}
	public function adding(){

		//过滤空字符
		$data['pid']=$_POST['pid'];
		$data['name']=trim($_POST['name']);
		$data['title']=trim($_POST['title']);
		$data['level']=$_POST['level'];
		$data['status']=1;
		//判断数据
		if($data['pid']==null){$this->error('请选择父节点');}
		if($data['name']==null){$this->error('模块为必填项');}
		if(!preg_match('/^[\w]*$/',$_POST['name'])){$this->error('模块格式不正确');}
		if($data['title']==null){$this->error('名称为必填项');}
		if($data['level']==null){$this->error('请选择节点等级');}

		$data['level']=($data['pid']==0)?1:$data['level'];

		$node=M('Node');$res=$node->add($data);
		if($res){$this->success('添加成功','index');}else{$this->error('添加失败');}

	}

	//权限等极值
	protected function level(){
		$arr=array(
			1=>'项目',2=>'模块',3=>'方法'
		);
		return $arr;
	}
	//父节点列表
	protected function pid(){
		$node=M('Node');
		$list=$node->getField('id,title');
		if($list){
			$list=$this->pidname($list);	//获得父节点名并组合
			$list=$this->pidsort($list);	//按照模块进行排序
			return $list;
		}else{
			return array(0=>'无父级');
		}
	}
	protected function pidname($list){
		$node=M('Node');

		foreach($list as $k=>$v){
			$res=$node->where(array('id'=>$k))->field('pid')->find();	//获得PID
			if($res['pid']!=0){
				$result=$node->where(array('id'=>$res['pid']))->field('title,pid')->find();	//获得名称
				$list[$k]=$result['title'].'→'.$v;

				if($result['pid']!=0){
					$result=$node->where(array('id'=>$result['pid']))->field('pid,title')->find();
					$list[$k]=$result['title'].'→'.$list[$k];
				}

			}
		}		
		return $list;
	}

	//节点排序
	protected function pidsort($list){
		$node=M('Node');

		$arr=array(0=>'无父级');	//要返回的数组
		foreach($list as $k=>$v){
			$res=$node->where('id='.$k)->field('level')->find();
			if($res['level']==1){
				$arr[$k]=$v;
			}
			//如果是模块则查找该栏目下方法
			if($res['level']==2){
				$arr[$k]=$v;	//将模块存入数组
				$son=$node->where('pid='.$k)->field('id')->select();
				for($i=0;$i<count($son);$i++){
					$arr[$son[$i]['id']]=$list[$son[$i]['id']];
				}
			}
		}

		return $arr;
	}

}